﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using WickedFlame.Core.Tools;
using WickedFlame.Util.Controls;

namespace WickedFlame.VecDraw.Plugins
{
    /// <summary>
    /// Interaction logic for ColorSelector.xaml
    /// </summary>
	public partial class MainColorSelector : HeaderedToolItem
    {
		double colorAlpha = 255;

        public MainColorSelector()
        {
            InitializeComponent();
			
			SelectedColor = ((SolidColorBrush)ShapePropertySettings.Settings.Fill).Color;
        }
		
		public Color SelectedColor
		{
			get 
            {
				return (Color)GetValue(SelectedColorProperty);
            }
			set
			{
				SetValue(SelectedColorProperty, value);
			}
		}

		public static readonly DependencyProperty SelectedColorProperty =
			DependencyProperty.Register("SelectedColor", typeof(Color), typeof(MainColorSelector), new PropertyMetadata(Colors.Transparent));



		public Brush FillBrush
		{
			get
			{
				return (Brush)GetValue(FillBrushProperty);
			}
			set
			{
				SetValue(FillBrushProperty, value);
			}
		}
		public static readonly DependencyProperty FillBrushProperty =
			DependencyProperty.Register("FillBrush", typeof(Brush), typeof(MainColorSelector),
			new FrameworkPropertyMetadata(Brushes.Transparent, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault,
			new PropertyChangedCallback(OnSelectedValueChanged)));

		public Brush StrokeBrush
		{
			get
			{
				return (Brush)GetValue(StrokeBrushProperty);
			}
			set
			{
				SetValue(StrokeBrushProperty, value);
			}
		}
		public static readonly DependencyProperty StrokeBrushProperty =
			DependencyProperty.Register("StrokeBrush", typeof(Brush), typeof(MainColorSelector),
			new FrameworkPropertyMetadata(Brushes.Transparent, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault,
			new PropertyChangedCallback(OnSelectedValueChanged)));

		protected override void OnPropertyChanged(DependencyPropertyChangedEventArgs e)
		{
			base.OnPropertyChanged(e);
			DependencyProperty prop = e.Property;
			if (prop == MainColorSelector.SelectedColorProperty)
			{
				//this.SelectedColor = (Color)e.NewValue;
				
				if (colorSelector.IsPrimarySelected)
					FillBrush = new SolidColorBrush(SelectedColor);
				else
					StrokeBrush = new SolidColorBrush(SelectedColor);

                if (AlphaSlider != null)
                    AlphaSlider.Value = SelectedColor.A;

				SetAlphaSliderColor();

				SetARGBSliderPosition();
			}
			else if (prop == MainColorSelector.FillBrushProperty)
			{
				if (colorSelector.IsPrimarySelected)
					SelectedColor = ((SolidColorBrush)e.NewValue).Color;
			}
			else if (prop == MainColorSelector.StrokeBrushProperty)
			{
				if(!colorSelector.IsPrimarySelected)
					SelectedColor = ((SolidColorBrush)e.NewValue).Color;
			}
		}

		private static void OnSelectedValueChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
		{
		}

		double Alpha
		{
			get
			{
				return colorAlpha;
			}
			set
			{
				if (value > 255)
					colorAlpha = 255;
				else if (value < 0)
					colorAlpha = 0;
				else
					colorAlpha = value;
			}
		}

        private void SetAlphaSliderColor()
        {
            Color startColor = Color.FromArgb(
                    (byte)0,
                    SelectedColor.R,
                    SelectedColor.G,
                    SelectedColor.B);

            Color endColor = Color.FromArgb(
                    (byte)255,
                    SelectedColor.R,
                    SelectedColor.G,
                    SelectedColor.B);

            LinearGradientBrush alphaBrush =
                new LinearGradientBrush(startColor, endColor,
                    new Point(0, 0), new Point(1, 0));

            if (AlphaBorder != null)
            {
                AlphaBorder.Background = alphaBrush;
            }
        }

		private void SetARGBSliderPosition()
		{
			try
			{
				if (rColorSlider != null &&
					gColorSlider != null &&
					bColorSlider != null &&
					AlphaSlider != null)
				{
					if (SelectedColor != Colors.Transparent)
					{
						AlphaSlider.IsEnabled = true;
						rColorSlider.IsEnabled = true;
						gColorSlider.IsEnabled = true;
						bColorSlider.IsEnabled = true;

						rColorSlider.Value = SelectedColor.R;
						gColorSlider.Value = SelectedColor.G;
						bColorSlider.Value = SelectedColor.B;
						AlphaSlider.Value = SelectedColor.A;

						txtColorA.Text = SelectedColor.A.ToString();
						txtColorR.Text = SelectedColor.R.ToString();
						txtColorG.Text = SelectedColor.G.ToString();
						txtColorB.Text = SelectedColor.B.ToString();
					}
					else
					{
						AlphaSlider.IsEnabled = false;
						rColorSlider.IsEnabled = false;
						gColorSlider.IsEnabled = false;
						bColorSlider.IsEnabled = false;

						rColorSlider.Value = SelectedColor.R;
						gColorSlider.Value = SelectedColor.G;
						bColorSlider.Value = SelectedColor.B;
						AlphaSlider.Value = SelectedColor.A;
					}
				}
			}
			catch (NullReferenceException) { }
		}

		private void Window_Loaded(object sender, RoutedEventArgs e)
		{
			Color startColor = Color.FromArgb(
					(byte)255,
					0,
					0,
					0);

			Color endColor = Color.FromArgb(
					(byte)255,
					255,
					0,
					0);

			if (rBorder != null)
			{
				rBorder.Background = new LinearGradientBrush(startColor, endColor,
						new Point(0, 0), new Point(1, 0));
				rColorSlider.Value = SelectedColor.R;
			}

			endColor = Color.FromArgb(
					(byte)255,
					0,
					255,
					0);

			if (gBorder != null)
			{
				gBorder.Background = new LinearGradientBrush(startColor, endColor,
						new Point(0, 0), new Point(1, 0));
				gColorSlider.Value = SelectedColor.G;
			}

			endColor = Color.FromArgb(
					(byte)255,
					0,
					0,
					255);

			if (bBorder != null)
			{
				bBorder.Background = new LinearGradientBrush(startColor, endColor,
						new Point(0, 0), new Point(1, 0));
				bColorSlider.Value = SelectedColor.B;
			}
		}

		private void AlphaSlider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
		{
			Alpha = AlphaSlider.Value;
			SelectedColor =
			   Color.FromArgb(
				   (byte)Alpha,
				   SelectedColor.R,
				   SelectedColor.G,
				   SelectedColor.B);

			if (txtColorA != null)
				txtColorA.Text = SelectedColor.A.ToString();
		}

		private void ColorSlider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
		{
			if (rColorSlider != null &&
				gColorSlider != null &&
				bColorSlider != null)
			{
				SelectedColor =
					Color.FromArgb(
					(byte)Alpha,
					   (byte)rColorSlider.Value,
					   SelectedColor.G,
					   SelectedColor.B);

				//txtColorA.Text = SelectedColor.A.ToString();
				txtColorR.Text = SelectedColor.R.ToString();
				txtColorG.Text = SelectedColor.G.ToString();
				txtColorB.Text = SelectedColor.B.ToString();
			}
		}

		private void rColorSlider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
		{
			if (rColorSlider != null)
			{
				SelectedColor =
					Color.FromArgb(
					(byte)Alpha,
					   (byte)rColorSlider.Value,
					   SelectedColor.G,
					   SelectedColor.B);

				if (txtColorR != null)
					txtColorR.Text = SelectedColor.R.ToString();
			}
		}

		private void gColorSlider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
		{
			if (gColorSlider != null)
			{
				SelectedColor =
					Color.FromArgb(
					(byte)Alpha,
					   SelectedColor.R,
					   (byte)gColorSlider.Value,
					   SelectedColor.B);

				if (txtColorG != null)
					txtColorG.Text = SelectedColor.G.ToString();
			}
		}

		private void bColorSlider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
		{
			if (bColorSlider != null)
			{
				SelectedColor =
					Color.FromArgb(
					(byte)Alpha,
					   SelectedColor.R,
					   SelectedColor.G,
					   (byte)bColorSlider.Value);

				if (txtColorB != null)
					txtColorB.Text = SelectedColor.B.ToString();
			}
		}

		private void txtColor_TextChanged(object sender, TextChangedEventArgs e)
		{
			string text = ((TextBox)sender).Text;

			try
			{
				if (sender == txtColorA)
					AlphaSlider.Value = int.Parse(text);
				else if (sender == txtColorR)
					rColorSlider.Value = int.Parse(text);
				else if (sender == txtColorB)
					bColorSlider.Value = int.Parse(text);
				else if (sender == txtColorG)
					gColorSlider.Value = int.Parse(text);
			}
			catch { }

			if (sender == txtColorA && text != AlphaSlider.Value.ToString())
				((TextBox)sender).Text = AlphaSlider.Value.ToString();
			else if (sender == txtColorR && text != rColorSlider.Value.ToString())
				((TextBox)sender).Text = rColorSlider.Value.ToString();
			else if (sender == txtColorB && text != bColorSlider.Value.ToString())
				((TextBox)sender).Text = bColorSlider.Value.ToString();
			else if (sender == txtColorG && text != gColorSlider.Value.ToString())
				((TextBox)sender).Text = gColorSlider.Value.ToString();
		}
		
		private void QuickColorSelector_MouseUp(object sender, MouseButtonEventArgs e)
		{
			if (colorSelector.IsPrimarySelected)
				FillBrush = (sender as Border).Background;
			else
				StrokeBrush = (sender as Border).Background;

            //AlphaSlider.Value = 255;
		}

		private void TransparentSelector_MouseUp(object sender, MouseButtonEventArgs e)
		{
			try
			{
				//SelectedColor = Colors.Transparent;
				if (colorSelector.IsPrimarySelected)
					FillBrush = Brushes.Transparent;
				else
					StrokeBrush = Brushes.Transparent;

                //AlphaSlider.Value = 0;
			}
			catch { }
		}
    }
}
